import java.util.Arrays;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-05-17
 * Time: 14:07
 */
public class test1 {
    public static void main(String[] args) {
        int[][] a = new int[2][];
        a[0] = new int[3];
        a[1] = new int[4];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static void main19(String[] args) {
        int[] a = null;
        System.out.println(a.length);
    }
    public static void main18(String[] args) {
        //二维数组转换成字符串
        int[][] a = new int[][]{{1,2,3},{4,5,6}};
        System.out.println(Arrays.deepToString(a));
    }
    public static void main17(String[] args) {
        //给你一个整数数组 arr，请你判断数组中是否存在连续三个元素都是奇数的情况：如果存在，请返回 true ；否则，返回 false 。
        int[] a = new int[]{1,2,3,5,7,8};
        boolean b = threeOddNum(a);
        System.out.println(b);
    }
    public static boolean threeOddNum(int[] a) {
        int count = 0;
        for (int i = 0; i < a.length - 2; i++) {
            if(a[i] % 2 !=0) {
                count++;
            }else {
                count = 0;
            }
            if(count == 3) {
                return true;
            }
        }
        return false;
    }

    public static void main16(String[] args) {
        //给定一个大小为 n 的数组，找到其中的多数元素。多数元素是指在数组中出现次数 大于 (n/2) 的元素。
        int[] a = new int[]{3,3,2,2,2,3,3};
        int b = findThree1(a);
        System.out.println(b);
    }
    //方法一：
    public static int findThree1(int[] a) {
        Arrays.sort(a);
        return a[a.length/2];
    }
    //方法二：
    public static int findThree(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int count = 0;
            for (int j = 0; j < a.length; j++) {
                if (a[i] == a[j]) {
                    count++;
                }
                if (count > (a.length / 2)) {
                    return a[i];
                }
            }
        }
        return -1;
    }

    public static void main15(String[] args) {
        //给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。找出那个只出现了一次的元素。
        //例如：输入[6,2,2,3,3],输出 1
        int[] a = new int[]{6,2,2,3,3};
        int b = findTwo1(a);
        System.out.println(b);
    }
    //方法一：
    public static int findTwo1(int[] a) {
        //异或求解
        int ret = 0;
        for (int i = 0; i < a.length; i++) {
            ret ^= a[i];
        }
        return ret;
    }
    //方法二:
    public static int findTwo(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int count = 0;
            for (int j = i + 1; j < a.length; j++) {
                if(a[i] == a[j]) {
                    count++;
                }
            }
            if(count == 0) {
                return a[i];
            }
        }
        return -1;
    }

    public static void main14(String[] args) {
        //给定一个整数数组 num 和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的那 两个 整数，并返回它们的数组下标。
        //你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。
        //输入：num = [2,7,11,15], target = 9   输出：[0,1]
        int[] num = new int[]{2,7,11,15};
        int target = 9;
        int[] a = findAdd(num,target);
        System.out.println(Arrays.toString(a));
    }
    public static int[] findAdd(int[] a,int b) {
        int[] c = new int[2];
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if(a[i] + a[j] == b) {
                    c[0] = i;
                    c[1] = j;
                    return c;
                }
            }
        }
        return c;
    }

    public static void main13(String[] args) {
        //给定一个整型数组, 实现冒泡排序(升序排序)
        int[] a = new int[]{1,33,22,66,44,6};
        mySort(a);
        System.out.println(Arrays.toString(a));
    }
    public static void mySort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if(a[j] > a[j+1]) {
                    int tmp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = tmp;
                }
            }
        }
    }

    public static void main12(String[] args) {
        //给定一个有序整型数组, 实现二分查找
        int[] a = new int[]{1,2,3,4,5};
        int b = 4;
        int c = findOne(a,b);
        System.out.println(c);
    }
    public static int findOne(int[] a,int b) {
        int left = 0;
        int right = a.length-1;
        while(left <= right) {
            int mid = (left + right) / 2;
            if(b > a[mid]) {
                left = mid + 1;
            }else if(b < a[mid]) {
                right = mid - 1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static void main11(String[] args) {
        //调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
        //如数组：[1,2,3,4,5,6]
        //调整后可能是：[1, 5, 3, 4, 2, 6]
        int[] a = new int[]{1,2,3,4,5,6};
        System.out.println("调整前：");
        System.out.println(Arrays.toString(a));
        moveOne(a);
        System.out.println("调整后：");
        System.out.println(Arrays.toString(a));
    }
    public static void moveOne(int[] a) {
        int left = 0;
        int right = a.length-1;
        while(left < right) {
            while(left < right && a[left] % 2 != 0) {
                left++;
            }
            while(left < right && a[right] % 2 == 0) {
                right--;
            }
                int tmp = a[left];
                a[left] = a[right];
                a[right] = tmp;
        }
    }

    public static void main10(String[] args) {
        //实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 ,
        // 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
        int[] a = new int[]{1,2,3};
        System.out.println("修改前：");
        System.out.println(Arrays.toString(a));
        transform(a);
        System.out.println("修改后：");
        System.out.println(Arrays.toString(a));
    }
    public static void transform(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = a[i] * 2;
        }
    }

    public static void main9(String[] args) {
        //二维数组遍历
        int[][] a = new int[][]{{1,2,3},{4,5,6}};
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }

        for(int[] b : a) {
            for(int c : b) {
                System.out.print(c + " ");
            }
            System.out.println();
        }

        System.out.println(Arrays.deepToString(a));
    }
    public static void main8(String[] args) {
        int[][] a = new int[2][3];
        int[][] b = new int[][]{{1,2,3},{4,5,6}};
        int[][] c = {{1,2,3},{4,5,6}};
        int[][] d;
        d = new int[2][3];
        int[][] f;
        f = new int[][]{{1,2,3},{4,5,6}};
    }
    public static void main7(String[] args) {
        //数组的拷贝
        int[] a = new int[]{1,2,3,4,5};
        int[] d = Arrays.copyOfRange(a,2,4);
        System.out.println(Arrays.toString(d));

        int[] c = Arrays.copyOf(a,a.length);
        System.out.println(Arrays.toString(c));

        int[] b = myCopyArray(a);
        System.out.println(Arrays.toString(b));
    }
    public static int[] myCopyArray(int[] a) {
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
        return b;
    }

    public static void main6(String[] args) {
        //查找数组指定元素
        int[] a = new int[]{1,2,3,4,5};
        int b = Arrays.binarySearch(a,4);
        int c = Arrays.binarySearch(a,2,4,3);
        System.out.println(b);
        System.out.println(c);
        //比较两个数组是否相等
        int[] arr1 = new int[]{1,2,3,4,5};
        int[] arr2 = new int[]{1,2,3,4,5};
        boolean d = Arrays.equals(arr1,arr2);
        System.out.println(d);
        //数组赋值
        int[] arr3 = new int[]{1,2,3,4,5};
        Arrays.fill(arr3,2,4,6);
        System.out.println(Arrays.toString(arr3));
        Arrays.fill(arr3,6);
        System.out.println(Arrays.toString(arr3));
        //数组转换成字符串
        int[] arr4 = new int[]{1,2,3,4,5};
        System.out.println(Arrays.toString(arr4));
    }
    public static void main5(String[] args) {
        //数组逆序
        int[] a = new int[]{1,2,3,4,5};
        move(a);
        System.out.println(Arrays.toString(a));
    }
    public static void move(int[] a) {
        int left = 0;
        int right = a.length - 1;
        while(left < right) {
            int tmp = a[left];
            a[left] = a[right];
            a[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main4(String[] args) {
        //冒泡排序（升序）
        int[] a = new int[]{1,22,5,33,66,55,9};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        mysort(a);
        System.out.println(Arrays.toString(a));
    }
    public static void mysort(int[] a) {
        boolean b = true;
        //轮数
        for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-1-i; j++) {
                if(a[j] > a[j+1]) {
                    int tmp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = tmp;
                    b = false;
                }
            }
            if(b == true) {
                return;
            }
        }
        return;
    }

    public static void main3(String[] args) {
        //查找数组中的元素，返回下标
        int[] a = new int[]{1,2,3,4,5};
        int d = Arrays.binarySearch(a,1,3,3);
        System.out.println(d);
        int c = Arrays.binarySearch(a,3);
        System.out.println(c);
        int b = 9;
        System.out.println(find(a,b));
        System.out.println(fingOne(a,b));
    }
    public static int fingOne(int[] a,int b) {
        int left = 0;
        int right = a.length - 1;
        while(left <= right) {
            int mid = (left + right) / 2;
            if(b > a[mid]) {
                left = mid + 1;
            }else if(b < a[mid]) {
                right = mid - 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
    public static int find(int[] a,int b) {
        for (int i = 0; i < a.length; i++) {
            if(b == a[i]) {
                return i;
            }
        }
        return -1;
    }
    
    public static void main2(String[] args) {
        int[] a = new int[]{1,3,5,7,9};
        String b = myToString(a);
        System.out.println(b);
    }
    public static String myToString(int[] a) {
        String arr = "[";
        for (int i = 0; i < a.length; i++) {
            arr += a[i];
            if(i != a.length-1) {
                arr += ", ";
            }
        }
        arr += "]";
        return arr;
    }

    public static void main1(String[] args) {
        //数组转字符串
        int[] a = new int[]{1, 2, 3, 4, 5};
        String b = Arrays.toString(a);
        System.out.println(b);
    }
}

